<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/4/24  14:57
 */
namespace app\admin\Controller;
use think\Controller;

class Link extends Common
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页
     */
    public function index()
    {
        $link = db('link');
        $ban = $link->select();
        $this->assign("res", $ban);
        return $this->fetch();
    }

    /**
     * 添加或修改
     */
    public function edit_link()
    {
        $id = input('id');
        if (request()->isPost()) {
            $data['status'] = input('status');
            $data['title'] = input('title');
            $data['content'] = input('content');
            $data['displayorder'] = input('displayorder');
            $data['url'] = input('url');
            $file = request()->file('bnimage');
            if (!empty($file)) {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if ($info) {
                    $data['pic'] = $info->getSaveName();
                } else {
                    $this->success($file->getError());
                }
            } else {
                $data['pic'] = input('bne');
            }
            if ($id != null) {
                $bnr = db('link')->where("id='$id'")->update($data);
            } else {
                $bnr = db('link')->insert($data);
            }
            $this->success("操作成功!", url('link/index'));
        }
        $link = db('link')->where("id='$id'")->find();
        $this->assign("link", $link);
        return $this->fetch();

    }

    /**
     * 删除
     */
    public function del_link()
    {
        $bnid = input('id');
        $link = db('link');
        $del = $link->where("id='$bnid'")->delete();
        if ($del) {
            $this->success("删除成功", url('link/index'));
        }

    }
}
